package web;

import javax.naming.InitialContext;
import javax.servlet.http.HttpServlet;

import javax.servlet.ServletException;
import javax.servlet.ServletConfig;
import java.io.IOException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Hashtable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import utils.ZaraException;

import entidadesVO.ArticuloVO;
import facade.Facade;


/**
 * Servlet Class
 *
 * @web.servlet              name="AddArticuloOFADSrvl"
 *                           display-name="Name for AddArticuloOFADSrvl"
 *                           description="Description for AddArticuloOFADSrvl"
 * @web.servlet-mapping      url-pattern="/AddArticuloOFADSrvl"
 * @web.servlet-init-param   name="A parameter"
 *                           value="A value"
 */
public class AddArticuloOFADSrvl extends HttpServlet {
	private Facade facade;

	public AddArticuloOFADSrvl() {
		// TODO Auto-generated constructor stub
		super();
	}

	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		// TODO Auto-generated method stub
		Hashtable props = new Hashtable();
		
		props.put(InitialContext.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
		props.put(InitialContext.PROVIDER_URL, "jnp://127.0.0.1:1099");
		String naming = "LaCorunaWebServer/FacadeBean/remote";
		try {
			//Objeto del tipo InitialContext
			InitialContext ic = new InitialContext(props);
			//Obtencion de referencia del session bean dentro del servidor de aplicaciones
			facade = (Facade)ic.lookup(naming);
			
			//Inicializacion de Datos de prueba
			//facade.inicializarDatos();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException,
		IOException {		
		String pagina = null;
		String[] artsRec = req.getParameterValues("refArt");
		Collection<ArticuloVO> artAux = new HashSet<ArticuloVO>();
		pagina = "/ofad.jsp";
		HttpSession ses = req.getSession(true);
		ArticuloVO[] artsVO = (ArticuloVO[])ses.getAttribute("articulos");				
		if(artsRec != null){
			for(int i=0; i<artsRec.length; i++) {
				boolean found = false;
				if(artsVO != null){				
					for (int j = 0; j < artsVO.length; j++) {
						ArticuloVO articuloVO = artsVO[j];
						if(articuloVO.getReferencia().equals(artsRec[i])){
							found = true;
							break;
						}
					}				
				}
				if(!found){			
					ArticuloVO art = null;
					try {
						art = facade.getArticulo(artsRec[i]);
					} catch (ZaraException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					if(art != null)
						artAux.add(art);
				}			
		    }
			
			if(artsVO != null){
				for (int i = 0; i < artsVO.length; i++) {
					artAux.add(artsVO[i]);				
				}
			}
			
			ses.setAttribute("articulos", artAux.toArray());
			System.out.println("agrega a session");
		}
		getServletConfig().getServletContext().getRequestDispatcher(pagina).forward(req,resp);
		//resp.sendRedirect(pagina);
	}
}
